home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 007a / cug317.zip / G4TDECOD.C < prev    next >
C/C++ Source or Header  |  1990-06-18  |  23KB  |  649 lines

  1. /*    $Id: g4tdecod.c 1.2 90/06/09 18:24:25 marking Exp $
  2.  *
  3.  NAME
  4.  *    g4tdecod.c -- decode group 4 data using tables
  5.  *
  6.  TYPE
  7.  *    C procedures
  8.  *
  9.  SYNOPSIS
  10.  *    char    g4i_initialize (short image_width, short image_length);
  11.  *    char    g4i_decode (void);
  12.  *
  13.  DESCRIPTION
  14.  *    In order to acquire data from the image and to return run lengths and
  15.  *    new line information, these routines invoke procedures provided by the
  16.  *    caller. These caller-provided procedures are invoked throught pointers
  17.  *    which have been stuffed by the caller with the procedure addresses.
  18.  *    To acquire a new data byte, g4i_decode () calls (*p_g41_next_byte) ().
  19.  *    To report the decoding of a black or white run, the routines
  20.  *    (*p_decode_black) () or (*p_decode_white) () are called.
  21.  *
  22.  RETURNS
  23.  *    Initialization always returns zero.
  24.  *
  25.  *    For decoding,
  26.  *        0    end of image reached
  27.  *        -1    on error (bad data)
  28.  *    The decode loop will be prematurely terminated if decode_return is
  29.  *    set to not zero, and the value of decode_return will be returned.
  30.  *    No code here does this, but it might be useful under certain
  31.  *    circumstances.
  32.  *
  33.  LEGAL
  34.  *    Copyright 1989, 1990 Michael P. Marking, Post Office Box 8039,
  35.  *    Scottsdale, Arizona 85252-8039. All rights reserved.
  36.  *
  37.  *    License is granted by the copyright holder to distribute and use this
  38.  *    code without payment of royalties or the necessity of notification as
  39.  *    long as this notice (all the text under "LEGAL") is included.
  40.  *
  41.  *    Reference: $Id: g4tdecod.c 1.2 90/06/09 18:24:25 marking Exp $
  42.  *
  43.  *    This program is offered without any warranty of any kind. It includes
  44.  *    no warranty of merchantability or fitness for any purpose. Testing and
  45.  *    suitability for any use are the sole responsibility of the user.
  46.  * 
  47.  HISTORY
  48.  *    $Log:    g4tdecod.c $
  49.  * Revision 1.2  90/06/09  18:24:25  marking
  50.  * clean up comments for release
  51.  * 
  52.  * Revision 1.1  89/06/30  17:00:00  marking
  53.  * Initial revision
  54.  * 
  55.  *
  56.  NOTES
  57.  *
  58.  PORTABILITY
  59.  *    Tested using Microsoft C 5.1. Some memory models may not work due to
  60.  *    the large decoding arrays.
  61.  *
  62.  *    There is a non-portable use of "global" variables in the file g3g4.h,
  63.  *    about which a minority of compilers will justifiably complain. Certain
  64.  *    variables are declared in g3g4.h without extern keywords. Strictly
  65.  *    speaking, they should be declared extern in all but one module, but
  66.  *    that would require complication of g3g4.h. If it gets past your
  67.  *    compiler and linker, you can probably ignore it.
  68.  *
  69.  SEE ALSO
  70.  *    g3tdecod.c -- decode group 3 image using tables
  71.  *    builddec.c -- build image decoding tables
  72.  *
  73.  INFORMATION
  74.  *    Although there is no support offered with this program, the author will
  75.  *    endeavor to correct errors. Updates will also be made available from
  76.  *    time to time.
  77.  *
  78.  *    Contact: Michael P. Marking, Post Office Box 8039, Scottsdale, Arizona
  79.  *    85252-8039 USA. Replies are not guaranteed to be swift. Beginning
  80.  *    July 1990, e-mail may be sent to uunet!ipel!marking.
  81.  *
  82.  *    Also beginning in July 1990, this code will be archived at the
  83.  *    ipel!phoenix BBS in file g3g4.zoo. The 24-hour telephone number
  84.  *    for 300/1200/2400 is (602)274-0462. When logging in, specify user
  85.  *    "public", system "bbs", and password "public".
  86.  *
  87.  *    This code is also available from the C Users Group in volume 317.
  88.  */
  89.  
  90. #include "g3g4.h"
  91.  
  92. /* #define TRACE 1 */
  93. #define TRACE_BEGIN 0
  94. #define TRACE_END 30000
  95.  
  96. /* implementation limits: Due to the sizes of arrays and variables, and not
  97.    due to any restrictions in the algorithm, the following limits exist:
  98.      maximum number of pixels per row: 65533
  99.      maximum number of rows per image: none
  100.      maximum or minimum k-factor: none
  101.      maximum number of runs per row: 16382 white, 16382 black
  102.    To increase (or decrease) these limits, it will be necessary to play with
  103.    array and variable sizes.  On segmented machines (such as the 8086), a
  104.    different memory model may be necessary.  The algorithm itself has no
  105.    limits on image size or complexity, and the stack requirements are in-
  106.    sensitive to changes in these limits or to image complexity. */
  107.  
  108. #define EVEN 0
  109. #define ODD 1
  110.  
  111. static short a0, a1, a2, b0, b1, b2, bit_number, code_byte;
  112. static unsigned char color, current_row, mode, next_state;
  113. static unsigned short even_runs [32768], odd_runs [32768];
  114. static unsigned short even_index, odd_index;
  115. static short column_limit;
  116. static short row_number = 0;
  117.   /* Depending as current_row == EVEN or current_row == ODD, the runs of the
  118.      current row are represented in even_runs [] or odd_runs [].  The white
  119.      runs have even subscripts and the black runs have odd subscripts.  The
  120.      values of the array elements are the offsets of the beginnings of the
  121.      corresponding runs from the beginning of the row.  As defined by the
  122.      specification,
  123.     a0 is the reference or starting changing element on the coding line.
  124.         It may be considered the "current position".
  125.     a1 is the next changing element to the right of a0 on the coding line.
  126.     a2 is the next changing element to the right of a1 on the coding line.
  127.     b1 is the first changing element on the reference line to the right of
  128.         a0 and of opposite color to a0.
  129.     b2 is the next changing element to the right of b1 on the reference
  130.         line.
  131.      Furthermore,
  132.     b0 is the "previous" value of b1. 
  133.      Depending as current_row == EVEN or == ODD, even_index or odd_index is
  134.      the subscript of the entry in even_runs [] or odd_runs [] corresponding
  135.      to the run containing the current value of a0, and its counterpart cor-
  136.      responds to the run containing b1. */
  137.  
  138. extern unsigned char huge null_mode [] [256];
  139.   /* One of the entries PASS_MODE, HORIZONTAL_MODE, etc, or zero.  If the entry
  140.      is zero, then the bit string is indigestible and null_mode_next_state [][]
  141.      is consulted to decode the subsequent byte.  The row number (first sub-
  142.      script) corresponds to the assumption about the state of the machine after
  143.      decoding the previous codeword.  If < 8, then it refers to the starting
  144.      bit number for this codeword.  If > 7, it implies a bit string to be pre-
  145.      fixed to the first bit of the current byte.  The column number (second
  146.      subscript) refers to the value of the current byte. */
  147.  
  148. extern unsigned char huge null_mode_next_state [] [256];
  149. extern unsigned char huge horiz_mode [] [256];
  150. extern unsigned char huge horiz_mode_next_state [] [256];
  151.  
  152. static void new_row (void);
  153. static short decode_white_run (void);
  154. static short decode_black_run (void);
  155.  
  156. static char decode_return;
  157.  
  158. /* g4i_decode () successively invokes (*p_decode_next_byte) () for each byte of the
  159.    encoded image, and calls (*p_decode_white) () or (*p_decode_black) () as
  160.    required to return the image contents on a run-by-run basis. */
  161. char g4i_decode ()
  162. {
  163.   /* At the beginning of this routine, we are in the NULL mode, which is to
  164.      to say that no codewords are currently understood or digested.  The
  165.      variable bit_number has been initialized to zero, to indicate that the
  166.      next (first) codeword is to begin with bit zero of the next code byte.
  167.      This betrays an assumption that all images begin on byte boundaries, a
  168.      condition that can be changed by arranging for bit_number to be set
  169.      otherwise by g4i_initialize () or elsewhere. */
  170.   while (!decode_return)
  171.   {
  172.     /* No codewords have been digested, so no mode is currently understood.
  173.        It is possible, however, that bits have been left over from the last
  174.        code byte, and that these must be conceptually prefixed to the next
  175.        bits at the beginning of the next code byte to establish which code
  176.        word is next.  The decision is made based on bit_number, which is
  177.        zero if the codeword begins on a byte boundary; 1, 2, ... 7 if the
  178.        codeword begins with bit 1, 2, ... 7; or > 7 if the codeword begins
  179.        with bit zero but is assumed to have a non-null bits string prefix.
  180.        In any case, the bit_number corresponds to a row in the decoding
  181.        tables. */
  182.     if (bit_number == 0 || bit_number > 7) /* the code word will begin on
  183.       the first bit of the next code byte, possibly with a bit string
  184.       implicitly prefixed */
  185.     {
  186.       code_byte = (*p_decode_next_byte) (); /* invoke a user-supplied routine
  187.         to provide the next byte of the encoded image */
  188.       if (code_byte < 0) /* implying end-of-file or some error in
  189.         reading the next code byte */
  190.         return (ERROR_NEXT_BYTE);
  191.     }
  192.     /* else 0 < bit_number < 8, implying that the next code word begins within
  193.       the current code byte; that is, a new byte need not be fetched */
  194.     mode = null_mode [bit_number] [code_byte];
  195.     next_state = null_mode_next_state [bit_number] [code_byte];
  196.       /* The above two index operations involve muliplication (by the compiler)
  197.          of the first subscript by 256, a procedure obviously a candidate for
  198.      optimization in many architectures.  It may be worthwhile to hand-code
  199.      this calculation, since this is a fairly tight loop.  Check also that
  200.      the compiler does the calculation but once and saves the result in a
  201.      temporary, since the two arrays are of identical size. */
  202.     if (mode == NULL_MODE) /* the codeword (or partial codeword) is
  203.       indigestible */
  204.     {
  205.       bit_number = next_state; /* to be continued... */
  206.     }
  207.     else
  208.     {
  209.       if (current_row == EVEN)
  210.       {
  211.     b0 = b1;
  212.     b1 = odd_runs [odd_index];
  213.     if ((b1 <= a0) && a0)
  214.     {
  215.       odd_index += 2;
  216.       b0 = odd_runs [odd_index - 1];
  217.       b1 = odd_runs [odd_index];
  218.     }
  219.     b2 = odd_runs [odd_index + 1];
  220.       }
  221.       else /* current_row == ODD */
  222.       {
  223.     b0 = b1;
  224.     b1 = even_runs [even_index];
  225.     if ((b1 <= a0) && a0)
  226.     {
  227.       even_index += 2;
  228.       b0 = even_runs [even_index - 1];
  229.       b1 = even_runs [even_index];
  230.     }
  231.     b2 = even_runs [even_index + 1];
  232.       }
  233.       #if defined (TRACE)
  234.     if (row_number >= TRACE_BEGIN && row_number <= TRACE_END)
  235.     {
  236.       if (current_row == EVEN)
  237.       {
  238.         printf ("\n ref_index=%hd b1=%hd b2=%hd a0=%hd curr[%hd]=%hd "
  239.           "color=%hd ",
  240.           odd_index, b1, b2, a0, even_index, even_runs [even_index],
  241.           (short) color);
  242.       }
  243.       else /* current_row == ODD */
  244.       {
  245.         printf ("\n ref_index=%hd b1=%hd b2=%hd a0=%hd curr[%hd]=%hd "
  246.           "color=%hd ",
  247.           even_index, b1, b2, a0, odd_index, odd_runs [odd_index],
  248.           (short) color);
  249.       }
  250.     }
  251.       #endif
  252.       switch (mode)
  253.       {
  254.         case PASS_MODE: /* skip (pass) two color changes on the previous row */
  255.       #if defined (TRACE)
  256.             if (row_number >= TRACE_BEGIN && row_number <= TRACE_END)
  257.               printf (" P ");
  258.       #endif
  259.           if (color == WHITE) (*p_decode_white) ((short) (b2 - a0));
  260.           else /* color == BLACK */ (*p_decode_black) ((short) (b2 - a0));
  261.       a0 = b2;
  262.       if (current_row == EVEN)
  263.       {
  264.         odd_index += 2;
  265.         b1 = odd_runs [odd_index];
  266.       }
  267.       else /* current_row == ODD */
  268.       {
  269.         even_index += 2;
  270.         b1 = even_runs [even_index];
  271.       }
  272.           bit_number = next_state;
  273.           break;
  274.         case HORIZONTAL_MODE: /* revert to 1-dimensional modified Huffman
  275.           encoding for a pair of runs */
  276.           bit_number = next_state;
  277.       #if defined (TRACE)
  278.             if (row_number >= TRACE_BEGIN && row_number <= TRACE_END)
  279.               printf (" H ");
  280.           #endif
  281.           if (color == WHITE)
  282.           {
  283.         short black_runlength, white_runlength;
  284.             white_runlength = decode_white_run ();
  285.             (*p_decode_white) ((short) white_runlength);
  286.         a1 = (a0 += white_runlength);
  287.             black_runlength = decode_black_run ();
  288.             (*p_decode_black) ((short) black_runlength);
  289.         a2 = (a0 += black_runlength);
  290.           }
  291.           else /* color == BLACK */
  292.           {
  293.         short black_runlength, white_runlength;
  294.         black_runlength = decode_black_run ();
  295.         (*p_decode_black) ((short) black_runlength);
  296.         a1 = (a0 += black_runlength);
  297.         white_runlength = decode_white_run ();
  298.         (*p_decode_white) ((short) white_runlength);
  299.         a2 = (a0 += white_runlength);
  300.           }
  301.           if (current_row == EVEN)
  302.           {
  303.         even_runs [++even_index] = a1;
  304.         even_runs [++even_index] = a2;
  305.         while (a0 > odd_runs [odd_index]) odd_index += 2;
  306.         b1 = odd_runs [odd_index];
  307.           }
  308.           else /* current_row == ODD */
  309.           {
  310.         odd_runs [++odd_index] = a1;
  311.         odd_runs [++odd_index] = a2;
  312.         while (a0 > even_runs [even_index]) even_index += 2;
  313.         b1 = even_runs [even_index];
  314.           }
  315.           break;
  316.         case VERTICAL_V0_MODE: /* the next color change begins at the same
  317.           location as in the previous row */
  318.       #if defined (TRACE)
  319.             if (row_number >= TRACE_BEGIN && row_number <= TRACE_END)
  320.               printf (" V0 ");
  321.       #endif
  322.       if (color == WHITE)
  323.       {
  324.         (*p_decode_white) ((short) (b1 - a0));
  325.         color = BLACK;
  326.       }
  327.       else /* color == BLACK */
  328.       {
  329.         (*p_decode_black) ((short) (b1 - a0));
  330.         color = WHITE;
  331.       }
  332.       a0 = b1; 
  333.           if (current_row == EVEN)
  334.           {
  335.         even_runs [++even_index] = a0;
  336.         odd_index++;
  337.           }
  338.           else /* current_row == ODD */
  339.           {
  340.         odd_runs [++odd_index] = a0;
  341.         even_index++;
  342.           }
  343.           bit_number = next_state;
  344.           break;
  345.         case VERTICAL_VR1_MODE: /* the next color change begins one pixel to the
  346.           right of its location on the previous row */
  347.       #if defined (TRACE)
  348.             if (row_number >= TRACE_BEGIN && row_number <= TRACE_END)
  349.               printf (" VR1 ");
  350.       #endif
  351.       if (color == WHITE)
  352.       {
  353.         (*p_decode_white) ((short) (b1 - a0 + 1));
  354.         color = BLACK;
  355.       }
  356.       else /* color == BLACK */
  357.       {
  358.         (*p_decode_black) ((short) (b1 - a0 + 1));
  359.         color = WHITE;
  360.       }
  361.       a0 = b1 + 1; 
  362.           if (current_row == EVEN)
  363.           {
  364.         even_runs [++even_index] = a0;
  365.         odd_index++;
  366.           }
  367.           else /* current_row == ODD */
  368.           {
  369.         odd_runs [++odd_index] = a0;
  370.         even_index++;
  371.           }
  372.           bit_number = next_state;
  373.           break;
  374.         case VERTICAL_VR2_MODE: /* the next color change begins two pixels to
  375.           the right of its location on the previous row */
  376.       #if defined (TRACE)
  377.             if (row_number >= TRACE_BEGIN && row_number <= TRACE_END)
  378.               printf (" VR2 ");
  379.       #endif
  380.       if (color == WHITE)
  381.       {
  382.         (*p_decode_white) ((short) (b1 - a0 + 2));
  383.         color = BLACK;
  384.       }
  385.       else /* color == BLACK */
  386.       {
  387.         (*p_decode_black) ((short) (b1 - a0 + 2));
  388.         color = WHITE;
  389.       }
  390.       a0 = b1 + 2; 
  391.           if (current_row == EVEN)
  392.           {
  393.         even_runs [++even_index] = a0;
  394.         odd_index++;
  395.           }
  396.           else /* current_row == ODD */
  397.           {
  398.         odd_runs [++odd_index] = a0;
  399.         even_index++;
  400.           }
  401.           bit_number = next_state;
  402.           break;
  403.         case VERTICAL_VR3_MODE: /* the next color change begins three pixels to
  404.           the right of its location on the previous row */
  405.       #if defined (TRACE)
  406.             if (row_number >= TRACE_BEGIN && row_number <= TRACE_END)
  407.               printf (" VR3 ");
  408.       #endif
  409.       if (color == WHITE)
  410.       {
  411.         (*p_decode_white) ((short) (b1 - a0 + 3));
  412.         color = BLACK;
  413.       }
  414.       else /* color == BLACK */
  415.       {
  416.         (*p_decode_black) ((short) (b1 - a0 + 3));
  417.         color = WHITE;
  418.       }
  419.       a0 = b1 + 3; 
  420.           if (current_row == EVEN)
  421.           {
  422.         even_runs [++even_index] = a0;
  423.         odd_index++;
  424.           }
  425.           else /* current_row == ODD */
  426.           {
  427.         odd_runs [++odd_index] = a0;
  428.         even_index++;
  429.           }
  430.           bit_number = next_state;
  431.           break;
  432.         case VERTICAL_VL1_MODE: /* the next color change begins one pixel to the
  433.           left of its location on the previous row */
  434.       #if defined (TRACE)
  435.             if (row_number >= TRACE_BEGIN && row_number <= TRACE_END)
  436.               printf (" VL1 ");
  437.       #endif
  438.       if (color == WHITE)
  439.       {
  440.         (*p_decode_white) ((short) (b1 - a0 - 1));
  441.         color = BLACK;
  442.       }
  443.       else /* color == BLACK */
  444.       {
  445.         (*p_decode_black) ((short) (b1 - a0 - 1));
  446.         color = WHITE;
  447.       }
  448.       a0 = b1 - 1; 
  449.           if (current_row == EVEN)
  450.           {
  451.         even_runs [++even_index] = a0;
  452.         odd_index++;
  453.           }
  454.           else /* current_row == ODD */
  455.           {
  456.         odd_runs [++odd_index] = a0;
  457.         even_index++;
  458.           }
  459.           bit_number = next_state;
  460.           break;
  461.         case VERTICAL_VL2_MODE: /* the next color change begins two pixels to
  462.           the left of its location on the previous row */
  463.       #if defined (TRACE)
  464.             if (row_number >= TRACE_BEGIN && row_number <= TRACE_END)
  465.               printf (" VL2 ");
  466.       #endif
  467.       if (color == WHITE)
  468.       {
  469.         (*p_decode_white) ((short) (b1 - a0 - 2));
  470.         color = BLACK;
  471.       }
  472.       else /* color == BLACK */
  473.       {
  474.         (*p_decode_black) ((short) (b1 - a0 - 2));
  475.         color = WHITE;
  476.       }
  477.       a0 = b1 - 2; 
  478.           if (current_row == EVEN)
  479.           {
  480.         even_runs [++even_index] = a0;
  481.         if (a0 < b0) odd_index--;
  482.         else odd_index++;
  483.           }
  484.           else /* current_row == ODD */
  485.           {
  486.         odd_runs [++odd_index] = a0;
  487.         if (a0 < b0) even_index--;
  488.         else even_index++;
  489.           }
  490.           bit_number = next_state;
  491.           break;
  492.         case VERTICAL_VL3_MODE: /* the next color change begins three pixels to
  493.           the left of its location on the previous row */
  494.       #if defined (TRACE)
  495.             if (row_number >= TRACE_BEGIN && row_number <= TRACE_END)
  496.               printf (" VL3 ");
  497.       #endif
  498.       if (color == WHITE)
  499.       {
  500.         (*p_decode_white) ((short) (b1 - a0 - 3));
  501.         color = BLACK;
  502.       }
  503.       else /* color == BLACK */
  504.       {
  505.         (*p_decode_black) ((short) (b1 - a0 - 3));
  506.         color = WHITE;
  507.       }
  508.       a0 = b1 - 3; 
  509.           if (current_row == EVEN)
  510.           {
  511.         even_runs [++even_index] = a0;
  512.         if (a0 < b0) odd_index--;
  513.         else odd_index++;
  514.           }
  515.           else /* current_row == ODD */
  516.           {
  517.         odd_runs [++odd_index] = a0;
  518.         if (a0 < b0) even_index--;
  519.         else even_index++;
  520.           }
  521.           bit_number = next_state;
  522.           break;
  523.         case EXT_MODE_UNCOMPRESSED: /* enter extension type 7 ("111"), an
  524.           uncompressed encoding scheme */
  525.           return (ERROR_UNSUPPORTED_EXTENSION);
  526.           break;
  527.         case ERROR_MODE: /* The bit pattern found corresponds to an unknown or
  528.           invalid codeword.  This MAY be one of the seven possible extensions
  529.           not defined by the specification. */
  530.           return (ERROR_INVALID_CODEWORD);
  531.           break;
  532.         case ERROR_MODE_1: /* assumed in this implementation to be equivalent
  533.           to EOFB (end-of-facsimile-block) */
  534.           return (RETURN_OK);
  535.           break;
  536.         default: /* we should never get here; if we do, the tables are bad */
  537.           return (ERROR_PROGRAM_LOGIC);
  538.           break;
  539.       }
  540.     }
  541.     if (a0 >= column_limit) new_row ();
  542.   }
  543.   return (decode_return);
  544. }
  545.  
  546. /* g4i_initialize () is called to set up to decode a new image.  All of the
  547.    static data (flags, etc) for g4i_decode () are initialized, allowing the
  548.    decoding of multiple images in a run as long as g4i_initialize () is
  549.    called before each one. */
  550. char g4i_initialize (short image_width, short image_length)
  551. {
  552.   color = WHITE;
  553.   bit_number= 0;
  554.   current_row = ODD;
  555.   even_runs [0] = 0;
  556.   even_runs [1] = image_width; /* initial b1 */
  557.   even_runs [2] = image_width; /* initial b2 */
  558.   odd_runs [0] = 0;
  559.   a0 = 0;
  560.   even_index = 1; odd_index = 0;
  561.   column_limit = image_width;
  562.   row_number = 0;
  563.   b1 = -1;
  564.   decode_return = 0;
  565.   return (0);
  566. }
  567.  
  568. static void new_row ()
  569. {
  570.   (*p_decode_new_row) ();
  571.   color = WHITE;
  572.   if (current_row == ODD)
  573.   {
  574.     current_row = EVEN;
  575.     odd_runs [++odd_index] = a0;
  576.     odd_runs [++odd_index] = a0;
  577.     odd_index = 1;
  578.     even_index = 0;
  579.   }
  580.   else /* current_row == EVEN */
  581.   {
  582.     current_row = ODD;
  583.     even_runs [++even_index] = a0;
  584.     even_runs [++even_index] = a0;
  585.     even_index = 1;
  586.     odd_index = 0;
  587.   }
  588.   a0 = 0;
  589.   b1 = -1;
  590.   #if defined (TRACE)
  591.     row_number++;
  592.   #endif
  593. }
  594.  
  595. static short run_length_table [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
  596.   13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  597.   32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
  598.   51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 128, 192, 256, 320,
  599.   384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024, 1088, 1152, 1216,
  600.   1280, 1344, 1408, 1472, 1536, 1600, 1664, 1728, 1792, 1856, 1920, 1984,
  601.   2048, 2112, 2176, 2240, 2304, 2368, 2432, 2496, 2560};
  602.  
  603. static short decode_black_run ()
  604. {
  605.   short runlength, accum_runlength = 0;
  606.   next_state = (unsigned char) (bit_number + 8);
  607.   for (;;) /* exit with "return" */
  608.   {
  609.     if (!bit_number) code_byte = (*p_decode_next_byte) ();
  610.      /* this will fetch a new byte
  611.         if the previous codeword ended on a byte boundary */
  612.     mode = horiz_mode [next_state] [code_byte];
  613.     next_state = horiz_mode_next_state [next_state] [code_byte];
  614.     if (mode) /* if digestible */
  615.     {
  616.       bit_number = next_state;
  617.       runlength = run_length_table [mode - 106];
  618.       accum_runlength += runlength;
  619.       if (runlength < 64) return (accum_runlength);
  620.       next_state += 8;
  621.     }
  622.     else bit_number = 0;
  623.   }
  624. }
  625.  
  626. static short decode_white_run ()
  627. {
  628.   short runlength, accum_runlength = 0;
  629.   next_state = (unsigned char) bit_number;
  630.   for (;;) /* exit with "return" */
  631.   {
  632.     if (!bit_number) code_byte = (*p_decode_next_byte) ();
  633.      /* this will fetch a new byte
  634.         if the previous codeword ended on a byte boundary */
  635.     mode = horiz_mode [next_state] [code_byte];
  636.     next_state = horiz_mode_next_state [next_state] [code_byte];
  637.     if (mode) /* if digestible */
  638.     {
  639.       bit_number = next_state;
  640.       runlength = run_length_table [mode - 2];
  641.       accum_runlength += runlength;
  642.       if (runlength < 64) return (accum_runlength);
  643.     }
  644.     else bit_number = 0;
  645.   }
  646. }
  647.  
  648. /*    end $RCSfile: g4tdecod.c $ */
  649.